//
//  RCCategoryView.m
//  RocketProject
//
//  Created by chen jiedan on 13-7-12.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCCategoryView.h"
#import "UIColor-Expanded.h"
#import "RCSearchMoreCell.h"
#import "TravelException.h"
#import "NSArray+EX.h"


#define kLeftTableTag           101
#define kRightTableTag          102

@interface RCCategoryView ()
@property(nonatomic,retain) UITableView *leftTableView;
@property(nonatomic,retain) UITableView *rightTableView;
@property(nonatomic,retain) RCSearchAllCityDataModel *dataModel;
@property(nonatomic,retain) NSMutableArray *selectItemArray;
@property(nonatomic,assign) NSInteger currentSelectedCell;

@end


@implementation RCCategoryView

@synthesize leftTableView = _leftTableView;
@synthesize rightTableView = _rightTableView;
@synthesize dataModel      = _dataModel;
@synthesize selectItemArray= _selectItemArray;
@synthesize currentSelectedCell = _currentSelectedCell;
@synthesize delegate            = _delegate;

- (id)initWithFrame:(CGRect)frame andDataSource:(RCSearchAllCityDataModel *)dataSource
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        if (!_leftTableView) {
            _leftTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 160, frame.size.height) style:UITableViewStylePlain];
        }
        _leftTableView.backgroundColor = [UIColor whiteColor];
        _leftTableView.delegate = self;
        _leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _leftTableView.dataSource = self;
        _leftTableView.tag = kLeftTableTag;
        [self addSubview:_leftTableView];
        
        
        if (!_rightTableView) {
            _rightTableView = [[UITableView alloc]initWithFrame:CGRectMake(160, 0, 160, frame.size.height) style:UITableViewStylePlain];
        }
        _rightTableView.backgroundColor = [UIColor colorWithHex:0xfffcf5ef];
        _rightTableView.delegate = self;
        _rightTableView.dataSource = self;
        _rightTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _rightTableView.tag = kRightTableTag;
        [self addSubview:_rightTableView];
        
        self.dataModel = dataSource;
        
        if (!_selectItemArray) {
            _selectItemArray = [[NSMutableArray alloc]initWithCapacity:1];
            NSArray *valueArray = [self.dataModel.itemValueArray safeObjectAtIndex:self.currentSelectedCell];
            if (valueArray != nil) {
                [_selectItemArray addObjectsFromArray:valueArray];
            }
        }
        
    }
    return self;
}


- (void) dealloc
{
    [_leftTableView release];
    [_rightTableView release];
    self.dataModel = nil;
    [_selectItemArray release];
    [super dealloc];
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/


#pragma -mark UITableView

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView.tag == kLeftTableTag) {
        if (nil == self.dataModel && nil == self.dataModel.itemKeyArray) {
            return 0;
        }else{
            return [self.dataModel.itemKeyArray count];
        }
    }else if (tableView.tag == kRightTableTag){
        return [_selectItemArray count];
    }
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView.tag == kLeftTableTag) {
//        if (self.viewType == SearchMoreTagsVCTypeCategory) {
            static NSString *cellIdentifier = @"LeftCategoryCell";
            RCSearchMoreCategoryCell *cell = (RCSearchMoreCategoryCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
            if (nil == cell) {
                cell = [[[RCSearchMoreCategoryCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]autorelease];
            }
            
            NSInteger row = [indexPath row];
            BOOL isSelect = self.currentSelectedCell == row ? YES:NO;
            NSString *key = nil;
            if (self.dataModel.itemKeyArray != nil) {
                key = [self.dataModel.itemKeyArray safeObjectAtIndex:row];
            }
            [cell setContentViewWithData:key isSelectedf:isSelect];
            return cell;
//        }else{
//            static NSString *cellIdentifier = @"LeftDistrictCell";
//            RCSearchMoreDistrictCell *cell = (RCSearchMoreDistrictCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//            if (nil == cell) {
//                cell = [[RCSearchMoreDistrictCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
//            }
//            
//            NSInteger row = [indexPath row];
//            BOOL isSelect = self.currentSelectedCell == row ? YES:NO;
//            NSString *key = nil;
//            if (self.dataModel.itemKeyArray != nil) {
//                key = [self.dataModel.itemKeyArray safeObjectAtIndex:row];
//            }
//            [cell setContentViewWithData:key isSelectedf:isSelect];
//            return cell;
//        }
    }else{
        static NSString *cellIdentifier = @"RightCellIdentifier";
        RCSearchRightCell *cell = (RCSearchRightCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (nil == cell) {
            cell = [[[RCSearchRightCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]autorelease];
        }
        
        NSInteger row = [indexPath row];
        RCSearchAllCityItem *value = nil;
        value = [self.selectItemArray safeObjectAtIndex:row];
        [cell setContentViewWithData:value];
        return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (tableView.tag == kLeftTableTag) {
        NSInteger row = [indexPath row];
        self.currentSelectedCell = row;
        NSArray *valueArray = [self.dataModel.itemValueArray safeObjectAtIndex:self.currentSelectedCell];
        if (valueArray != nil) {
            [_selectItemArray removeAllObjects];
            [_selectItemArray addObjectsFromArray:valueArray];
        }
        [_leftTableView reloadData];
        [_rightTableView reloadData];
    }else if (tableView.tag == kRightTableTag){
        NSString *categoryStr = [self.dataModel.itemKeyArray safeObjectAtIndex:self.currentSelectedCell];
        RCSearchAllCityItem *item = [_selectItemArray safeObjectAtIndex:[indexPath row]];
        if ([_delegate respondsToSelector:@selector(rcCategoryView:didSelectCategory:withItem:)]) {
            [_delegate rcCategoryView:self didSelectCategory:categoryStr withItem:item];
        }
    }
}


@end
